package servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.*;
import connection.DbConnection;
import java.sql.ResultSet;
import java.util.*;
import model.EmailingSystem;
import model.InternalMail;
import model.PollUserImpl;
import bean.Message;
import bean.PollUser;

public class SubmitVotingAdminServlet extends HttpServlet {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1119964869193819605L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost (request, response);
	}
	
	public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("GBK");
		
		// Error checking
		System.out.println("doPost method of SubmitVotingAdminServlet reached");
		
		// Get current poll
		if (request.getSession().getAttribute("selected_poll") != null) {
			String pollid = request.getSession().getAttribute("selected_poll").toString();
			
			// Error checking
			System.out.println("pollid: "+pollid);
			
			// Generate parameter array
			ArrayList<String> parameterArray = new ArrayList<String>();
			DbConnection db = new DbConnection();
			String sql = "Select Question.QuestionID from question, Poll_Question where Poll_Question.QuestionID = Question.QuestionID and Poll_Question.PollID = "+pollid;
			System.out.println("SubmitVotingAdminServlet SQL: "+sql);
			ResultSet rs = db.doSelect(sql);
			try {
				while (rs.next()) {
					System.out.println("Questionid: "+rs.getInt(1));
					parameterArray.add(rs.getString(1));
				}
			} catch (Exception e) {
				System.out.println("SQL ERROR in SubmitVotingAdminServlet.  Is there results? ");
			}
			
			// Error checking array
			for (int i = 0; i < parameterArray.size(); i++) {
				System.out.println("parameterArray at"+i+" is "+parameterArray.get(i));
			}
			
			// Generate status array based on parameter array
			ArrayList<Integer> statusArray = new ArrayList<Integer>();
			for (int i = 0; i < parameterArray.size(); i++) {
				//if (request.getParameter(parameterArray.get(i)).equals('))
				//statusArray.add(Integer.parseInt(request.getParameter(parameterArray.get(i))));
				if (request.getParameter(parameterArray.get(i)) != null) {
					statusArray.add(1);
				} else {
					statusArray.add(0);
				}
				//System.out.println("status: "+request.getParameter(parameterArray.get(i)));
			}
			
			// Error checking array
			for(int i = 0; i < statusArray.size(); i++) {
				System.out.println("statusArray for qid: "+parameterArray.get(i)+" is: "+statusArray.get(i));
			}
			
			// Update database for each parameterArray item
			String sql2;
			for (int i = 0; i < parameterArray.size(); i++) {
				sql2 = "Update Question set status = "+statusArray.get(i)+" where QuestionID = "+parameterArray.get(i);
				System.out.println("SubmitVotingAdminServlet update database status SQL: "+sql2);
				db.doUpdate(sql2);
			}
			
		}
		
		// Send back
		response.sendRedirect("administerVoting.jsp");
	}
}
